//
//  BookCell.swift
//  clreader
//
//  Created by colin on 2019/3/6.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

class BookCell: UITableViewCell {
    
    static let height:CGFloat = 120.0
    static let reuseId = "BookCell"
    
    private lazy var imageViewInfo = UIImageView()
    private lazy var imageViewTypeTag = UIImageView()
    private lazy var labelName = UILabel()
    private lazy var labelDesc = UILabel()
    private lazy var labelBuyed = UILabel()
    private lazy var labelPrice = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
        let content = CLTouchableView().then {
            
            // 图片
            self.imageViewInfo.do {
                $0.layer.masksToBounds = true
                $0.layer.cornerRadius = 3.0
            }
            $0.addSubview(self.imageViewInfo)
            self.imageViewInfo.snp.makeConstraints({ (make) in
                make.size.equalTo(CGSize(width: 107.0, height: 80.0))
                make.left.equalToSuperview().offset(15.0)
                make.centerY.equalToSuperview()
            })
            
            // 阴影背景
            let imageViewInfoShadow = UIView().then {
                $0.backgroundColor = UIColor.black
                $0.layer.cornerRadius = 3.0
                $0.layer.shadowColor = UIColor.black.cgColor
                $0.layer.shadowOffset = CGSize(width:2.0,height:4.0)
                $0.layer.shadowOpacity = 0.3 //阴影
            }
            $0.insertSubview(imageViewInfoShadow, belowSubview: self.imageViewInfo)
            imageViewInfoShadow.snp.makeConstraints({ (make) in
                make.edges.equalTo(self.imageViewInfo)
            })
            
            $0.addSubview(self.imageViewTypeTag)
            self.imageViewTypeTag.snp.makeConstraints({ (make) in
                make.left.equalTo(self.imageViewInfo.snp.left).offset(-5.0)
                make.bottom.equalTo(self.imageViewInfo.snp.bottom).offset(-5.0)
                make.size.equalTo(CGSize(width: 56.0, height: 19.0))
            })
            
            // 名称
            self.labelName.do {
                $0.numberOfLines = 2
                $0.textColor = UIColor(hex: "343434")
                $0.font = UIFont.systemFont(ofSize: 16.0, weight: .regular)
            }
            $0.addSubview(self.labelName)
            self.labelName.snp.makeConstraints({ (make) in
                make.left.equalTo(self.imageViewInfo.snp.right).offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.top.equalToSuperview().offset(15.0)
            })
            
            // 描述
            self.labelDesc.do {
                $0.textColor = UIColor(hex: "828282")
                $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
                $0.numberOfLines = 2
            }
            $0.addSubview(self.labelDesc)
            self.labelDesc.snp.makeConstraints({ (make) in
                make.left.equalTo(self.imageViewInfo.snp.right).offset(15.0)
                make.right.equalToSuperview().offset(-15.0)
                make.top.equalTo(self.labelName.snp.bottom).offset(5.0)
            })
            
            // 已购人数
            self.labelBuyed.do {
                $0.textColor = UIColor(hex: "828282")
                $0.font = UIFont.systemFont(ofSize: 12.0, weight: .light)
            }
            $0.addSubview(self.labelBuyed)
            self.labelBuyed.snp.makeConstraints({ (make) in
                make.left.equalTo(self.imageViewInfo.snp.right).offset(15.0)
                make.bottom.equalToSuperview().offset(-15.0)
            })
            
            // 价格
            self.labelPrice.do {
                $0.textColor = UIColor(hex: "343434")
            }
            $0.addSubview(self.labelPrice)
            self.labelPrice.snp.makeConstraints({ (make) in
                make.right.equalToSuperview().offset(-15.0)
                make.bottom.equalToSuperview().offset(-15.0)
            })
            
            // 分割线
            let line = UIView().then {
                $0.backgroundColor = UIColor(hex: "F0F0F0")
            }
            $0.addSubview(line)
            line.snp.makeConstraints({ (make) in
                make.left.equalTo(self.imageViewInfo.snp.right).offset(15.0)
                make.bottom.equalToSuperview()
                make.right.equalToSuperview().offset(-15.0)
                make.height.equalTo(0.5)
            })
            
        }
        self.contentView.addSubview(content)
        content.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
    }
    
    func refresh(data:JSONObject?, isShowSourceType:Bool = false) {
        
        self.labelName.text = data?["title"] as? String
        self.labelDesc.text = data?["marketing"] as? String
        
        // 已购数量
        if let trade_count = data?["trade_count"] as? Int {
            self.labelBuyed.text = "\(trade_count)人已购"
        } else {
            self.labelBuyed.text = ""
        }
        
        // 价格
        let price = data?["price"] as? Double
        self.labelPrice.attributedText = price?.priceAttr
        
        // 图片
        if let cover_url = data?["cover_url"] as? String,!cover_url.isEmpty,
            let baseUrl = URL(string: Router.baseUrl) {
            let imageUrl = baseUrl.appendingPathComponent(cover_url)
            self.imageViewInfo.kf.setImage(with: imageUrl)
        } else {
            self.imageViewInfo.image = nil
        }
        
        // 类型图片
        if let type = SourceType(type: data?["type"] as? Int),!type.tagName.isEmpty,
            let tagImage = UIImage(named: type.tagName), isShowSourceType {
            self.imageViewTypeTag.image = tagImage
            
            self.imageViewTypeTag.isHidden = false
        } else {
            self.imageViewTypeTag.isHidden = true
        }
        
        if let type = SourceType(type: data?["type"] as? Int), type == .专题 {
            self.labelBuyed.isHidden = true
            self.labelPrice.isHidden = true
        } else {
            self.labelBuyed.isHidden = false
            self.labelPrice.isHidden = false
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
